package hot100.h0912;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description EXCEL表列名称
 * @date 2024/9/12 22:14
 */
public class ExcelTable {
    public static void main(String[] args) {
        System.out.println(titleToNumber("AB"));
        System.out.println(convertToTitle(701));
    }

    public static String convertToTitle(int columnNumber) {
        //相当于26进制
        StringBuilder builder = new StringBuilder();
        while (columnNumber != 0) {
            columnNumber--; //关键步骤，转换为0基
            int i = columnNumber % 26;
            builder.append((char) (65 + i));
            columnNumber /= 26;
        }
        return builder.reverse().toString();
    }

    public static int titleToNumber(String columnTitle) {
        int sum = 0;
        int n = columnTitle.length();
        for (int i = 0; i < n; i++) {
            int value=(columnTitle.charAt(i)-'A'+1);
            sum=sum*26+value;
        }
        return sum;
    }
}
